import {
	isNumber
} from "./verify";

/**
 * 数组累加
 * @param {Array} arr = 数组
 * @return {string} 字符串
 */
export let total = (arr) => {
	var s = 0;
	arr.forEach(function(val, idx, arr) {
		s += val;
	}, 0);
	return s;
};
/**
 * 时间格式化
 * @param {object,string} date = 时间
 * @param {string} format = 'yyyy-MM-dd'
 * @return {string} 字符串
 */
export const dateFilter = (date, format = "yyyy-MM-dd") => {
	// format: string -> yyyy-MM-dd hh:mm:ss
	if (!date) return '暂无时间'
	if (typeof date === 'string') {
		date = date.replace(/-/g, '/').replace(/\./g, '/').replace('T', ' ')
		date = date.split('/')
		if(date.length>3){
			date.pop()
		}
		date =  date.join('/')
	}
	let d = new Date(date)
	let year = d.getFullYear()
	let month = (d.getMonth() + 1).toString().padStart(2, 0)
	let day = d.getDate().toString().padStart(2, 0)
	let hour = d.getHours().toString().padStart(2, 0)
	let minute = d.getMinutes().toString().padStart(2, 0)
	let second = d.getSeconds().toString().padStart(2, 0)

	let weekend = ['天', '一', '二', '三', '四', '五', '六']
	let week = d.getDay()
	return format.replace('yyyy', year)
		.replace('MM', month)
		.replace('dd', day)
		.replace('hh', hour)
		.replace('mm', minute)
		.replace('ss', second)
		.replace('week', '星期' + weekend[week])
}
/**
 * 路径过滤补全
 * @param {string} url = '字符串'
 * @return {string} 字符串
 */
export const imgFilter = url => {
	if (!url) return ''
	if (url.includes('http') || url.includes('static') || url.includes('data:image')) return url
	let baseUrl = uni.$config.baseUrl
	return baseUrl + url
}
/**
 * 手机号脱敏处理，保留前三后四
 * @param {string} value = 传入手机号
 * @return {string} 字符串
 */
export const phoneHide = (value) => {
	if (!value) return '';
	let str = value;
	str = str.replace(new RegExp("[^0-9]+", "g"), "")
	if (str.length == 11) {
		str = str.toString().replace(/(\d{3})\d*(\d{4})/, '$1****$2')
	} else {
		str = str.toString().replace(/(\d{3})\d*(\d{4})/, '$1******$2')
	}
	return str;
}
/**
 * 数字转大写
 * @param {string,number} temp = '字符串'
 * @return {string} 字符串
 */
export const numFormatter = (temp) => {
	if (temp === 0) return '零'
	let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
	let unit = ["", "十", "百", "千", "万"];
	let strArr = temp.toString().split("").reverse();
	let newNum = "";
	if (temp < 100) {
		for (var i = 0; i < strArr.length; i++) {
			newNum = (
				// temp为0时,newNum为空
				i == 0 && strArr[i] == 0 && strArr.length < 3 ? "" :
				(
					// temp为0时,newNum为空
					i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" :
					(
						// 10~19以内 不是10
						strArr[i] == 1 && strArr[i - 1] <= 9 && strArr[i - 1] != 0 ?
						unit[i] + changeNum[strArr[i - 1]] :
						// 10~19以内 是10
						(strArr[i] == 1 && strArr[i - 1] <= 9 && strArr[i - 1] == 0 && strArr.length < 3 ? unit[
								i]
							// 10~19以外的数字
							:
							changeNum[strArr[i]] +
							(
								strArr[i] == 0 ? unit[0] : unit[i]
							)
						)
					)
				)
			) + (strArr[i] == 1 && strArr[i - 1] <= 9 ? '' : newNum);
		}
		return newNum;
	} else {
		for (var i = 0; i < strArr.length; i++) {
			newNum = (i == 0 && strArr[i] == 0 ? '' : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" :
				changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
		}
		return newNum;
	}
}
/**
 * 获取延迟几个小时的描述
 * @param {string,date} time 时间
 * @param {number} hour 要延迟的小时数
 * @return {number} 时间戳
 */
export const timestamp = (time, hour = 0) => {
	let timestamp = new Date(dateFilter(time, 'yyyy/MM/dd hh:mm')).getTime()
	let curr = new Date().valueOf()
	let aDay = 3600 * 1000 * hour
	return (timestamp + aDay - curr) / 1000
}
/**
 * 比上期
 * @param {number} newNum = '本期数'
 * @param {number} oldNum = '上期数'
 * @param {string} str = '符号'
 */
export const coeff = (newNum, oldNum, str = '') => {
	let a = Number(Math.abs(newNum - oldNum))
	if (isNaN(a)) return ''
	let b = Boolean(a > 10000) ? Number(a / 10000).toFixed(2) + '万' : a.toFixed(2)
	return b + str
}
/**
 * 正负数
 * @param {number} newNum = '本期数'
 * @param {number} oldNum = '上期数'
 */
export const sign = (newNum, oldNum) => {
	// if(newNum-oldNum===0) return true
	return newNum - oldNum > 0
}
/**
 * 匹配超额省略加上单位
 * @param {number} Num = '源数值'
 */
export const matchingNnit = (Num) => {
	let a = Number(Num)
	let b = a > 10000 ? Number(a / 10000).toFixed(2) + '万' : a.toFixed(2)
	return b
}
/**
 * 匹配超额省略加上单位
 * @param {number} Num = '源数值'
 */
export const unitK = (Num) => {
	let a = Number(Num)
	if (isNaN(a)) {
		console.warn('要过滤的不是数字')
		return 0
	}
	let b = a > 1000 ? Number(a / 1000).toFixed(1) + 'k' : a
	return b
}
/**
 * 姓名截取
 * @param {string} name 姓名
 */
export function compellationCut(name) {
	// var reg = /(?<=.)./g;
	// return name.replace(reg, '*');
	return "*" + name.substring(1, name.length);
}
/**
 * 身份证号截取
 * @param {string} card 身份证号
 */
export function idCardCut(card) {
	return card.replace(/^(.{4})(?:\d+)(.{2})$/, "$1****$2")
}
/**
 * 手机号中间截取使用*号代替
 * @param {string} phone 手机号
 */
export function phoneCut(phone) {
	return phone.substr(0, 3) + '****' + phone.substr(7);
}
/**
 * 聊天时间过滤
 */
export const chatTime = (time, format = 'yyyy-MM-dd') => {
	if (!time) return ''
	if (typeof time === 'string') {
		time = time.replace(/-/g, '/').replace(/\./g, '/').replace('T', ' ')
		time = time.split('/')
		if(time.length>3){
			time.pop()
		}
		time =  time.join('/')
	}
	let date = new Date(time).getDate()
	let currentTime = new Date().getDate()
	let d = currentTime - date
	if (d === 0) {
		return dateFilter(time, 'hh:mm')
	} else if (d === 1) {
		return '昨天' + dateFilter(time, 'hh:mm')
	} else if (d === 2) {
		return '前天' + dateFilter(time, 'hh:mm')
	} else {
		return dateFilter(time, format)
	}

}
/**
 * 总计时间过滤
 */
export const totalTime = (time) => {
	let h = 3600
	let f = 60
	if (time > h) {
		let hh = parseInt(time / h)
		let ff = parseInt((time - hh * h) / f)
		return `${hh} 时${ff}`
	} else if (time > f) {
		let ff = parseInt(time / 60)
		let ss = (time - ff * f)
		return `${ff} 分${ss} 秒`
	} else {
		return time + ' 秒'
	}
}

/**
 * 数字价格过滤
 */
export const toFixedFilter = (num, n = 2) => {
	let number = parseFloat(num)
	if (isNaN(number)) {
		let a = 0
		a.toFixed(n)
		return
	}
	return number.toFixed(n)
}